package com.ijava.base;

import java.util.Date;
import java.sql.Timestamp;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ijava.base.domain.User;
import com.ijava.base.service.UserService;
import com.ijava.base.service.impl.UserServiceImpl;

public class SpringHibernate1 {
	Logger logger = LoggerFactory.getLogger(SpringHibernate1.class);

	@Test
	public void saveAndUpdate() {
		try {

			ApplicationContext context = new ClassPathXmlApplicationContext(
					new String[] { "applicationContext-base.xml" });

			UserService service = context.getBean(UserServiceImpl.class);

			for (int i = 0; i < 100; i++) {

				User user = new User("jrchens-" + (i+1), "ch666666", "jrchens-" + (i+1)
						+ "@126.com", "我的名子-" + (i+1), "13512536957", null, null);
				user.setCrTime(new Date());
				user.setCrUserId("x");
				service.save(user);
			}

//			User user = new User("jrchens", "ch666666", "jrchens@126.com",
//					"我的名子", "13512536957", null, null);
//			user.setCrTime(new Date());
//			user.setCrUserId("x");
//			service.save(user);
//			user.setPassword("cH666666");
//			service.update(user);

//			Assert.assertEquals("cH666666", user.getPassword());
		} catch (BeansException e) {
			logger.error("init error", e);
		} catch (Exception e) {
			logger.error("error", e);
		}
	}

	@Test
	public void find() {
		try {

			ApplicationContext context = new ClassPathXmlApplicationContext(
					new String[] { "applicationContext-base.xml" });

			UserService service = context.getBean(UserServiceImpl.class);

			User user = new User("jrchens", "ch666666", "jrchens@126.com",
					"我的名子", "13512536957l", null, null);
			user.setCrTime(new Date());
			user.setUserId("x");
			List list = service.findWithPaging(user);

			Assert.assertEquals(2d, list.size());
		} catch (BeansException e) {
			logger.error("init error", e);
		} catch (Exception e) {
			logger.error("error", e);
		}
	}
}
